package df.bean.process;

import df.applet.form.SummaryMonthlyFrm;
import df.bean.db.conn.DBConnection;
import df.bean.db.table.Batch;
import df.bean.obj.doctor.*;
import df.bean.obj.util.DialogBox;
import df.bean.obj.util.JDate;
import df.bean.obj.util.Variables;

public class ProcessTransMonthly extends Process {   
    String YYYY = "";
    String MM = "";
    private DoctorList drList = null;
    
    public ProcessTransMonthly(DBConnection conn) {
        super(conn);
        this.setDBConnection(conn);
    }
    public ProcessTransMonthly() {
        super();
    }
    @Override
    protected void finalize() {
        drList = null;
    }

    @Override
    public void run() {
        String lstCode = "";
        boolean ret = true;
        try {
            if (working) { 
                values.clear();
                values.add("---- Start ----");
                values.add(JDate.getDate());
                values.add(JDate.getTime());
                this.calculatorFrm.setTableValue(values);
                this.calculatorFrm.setRun();
                
                // Eash Hospital
                this.calculatorFrm.getLstHospitalCode().setSelectedIndex(0);
                this.calculatorFrm.setHospitalCode(this.calculatorFrm.getLstHospitalCode().getSelectedValue().toString());
                this.calculatorFrm.addDoctorGroupToList(Variables.getHospitalCode());

                // create batch // don't remove because we need to use static variable such as YYYY and MM
                Batch batch = new Batch(Variables.getHospitalCode(), this.getDBConnection());
                this.drList = new DoctorList(Variables.getHospitalCode(), this.getDBConnection());

                drList.newAllDoctor(Variables.getHospitalCode());
                MM = ((SummaryMonthlyFrm)this.calculatorFrm).getMM();
                YYYY = ((SummaryMonthlyFrm)this.calculatorFrm).getYYYY();
                
                this.getDBConnection().beginTrans();
                // Eash Doctor
                for (int j=0; j<this.calculatorFrm.getLstDoctorGroupCode().getModel().getSize(); j++) {
                    this.calculatorFrm.getLstDoctorGroupCode().setSelectedIndex(j);
                    lstCode = this.calculatorFrm.getLstDoctorGroupCode().getSelectedValue().toString();
                    this.calculatorFrm.setCount((j+1) + " : " + this.calculatorFrm.getLstDoctorGroupCode().getModel().getSize());

                    setDoctorList(drList);
                    if (!Calculate(YYYY, MM, Variables.getHospitalCode(), lstCode)) {
                        running = false; ret = false;
                    }
                    
                    if (!running) break;
                    if (!working) break;
                } 
                batch = null;

                working = false;
                values.clear();
                values.add("==== Stop ====");
                values.add(JDate.getDate());
                values.add(JDate.getTime());
                this.calculatorFrm.setTableValue(values);
                values = null; 
            }
        } catch (Exception e) {
            // TODO
            running = false;
            DialogBox.ShowError("Error in Process.Run() \n" + lstCode + "\n" + e.getMessage());
            System.out.print(e.getMessage());
            this.calculatorFrm.stopProcess();
        } finally {   
            if (running == true) { this.getDBConnection().commitTrans(); }
            if (running == false) { this.getDBConnection().rollBackTrans(); }
            if (running == true) { DialogBox.ShowInfo("     Completed !!!!   ");    }
            if (ret == false) { DialogBox.ShowError("      Fail !!!!  "); }
            this.calculatorFrm.setStop();
            this.calculatorFrm.stopProcess();
        }

        
    }
    
    @Override
    public boolean Calculate(String YYYY, String MM, String hospitalCode) {
    	System.out.println("call calculate in ProcessTransMonthly");
        boolean ret = false;
        CareProvider careProvider = null;
        int iCount = 0;
        ret = true;
        
        for (int i=0; i<this.getDoctorList().getDoctorLst().size(); i++) {
            careProvider = this.getDoctorList().getDoctorIDX(i);
            if (careProvider != null) {
                if (careProvider.computeTransMonthly()) {
                    iCount++;
                    ret = true;    
                } 
                else {  return false;   }
            }
        }

        return ret;
    } 
    
    // Use here
    public boolean Calculate(String YYYY, String MM, String hospitalCode, String doctorCode) {
        boolean ret = true;
        CareProvider careProvider = null;
        careProvider = this.getDoctorList().getDoctor(doctorCode);
        System.out.println("Monthly Process : "+careProvider);
        
        if (careProvider != null) {
            values.clear();
            values.add(JDate.getTime());
            values.add(careProvider.getDoctorCode());
            values.add(careProvider.getDoctor().getNameThai());
            if (careProvider.computeTransMonthly()) {
            	values.add("OK");
                //values.add(careProvider.getMessage());
                ret = true;    
            }else {  
                values.add("Fail !!!!  " + careProvider.getMessage());
                ret = false;   
            }
        }else{
        	values.add("else condition");
        }
        
        if (this.calculatorFrm != null) this.calculatorFrm.setTableValue(values);

        return ret;
    }
    
    public boolean Calculate(String hospitalCode) {
        boolean ret = true;
        CareProvider careProvider = null;
        
        if (careProvider == null) {
            if (careProvider.computeTransMonthly()) {
                ret = true;    
            }else {  
                ret = false;   
            }
        }
        return ret;
    }
}
